﻿using System;
using System.Windows.Forms;
using CuteSprite.Eme.Client;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Components;
using HmFramework.UI.Utils;

namespace CuteSprite.Eme.UI.Settlement
{
	public partial class FrmBsdSettlementBatch : HmBaseForm
	{
		#region -- 属性 --

		private DateTime _StartTime;

		private EmeBsdMisManagement _BsdMisManagement = null;

		/// <summary>BsdMIS客户端管理</summary>
		private EmeBsdMisManagement BsdMisManagement
		{
			get
			{
				if (_BsdMisManagement == null) { _BsdMisManagement = EmeRemotingHelper.MisServerManagement as EmeBsdMisManagement; }
				return _BsdMisManagement;
			}
		}

		#endregion

		#region -- 构造 --

		/// <summary>默认构造函数</summary>
		private FrmBsdSettlementBatch()
			: base()
		{
			InitializeComponent();
			InitForm();
		}

		/// <summary>创建窗体实例</summary>
		/// <param name="mis"></param>
		/// <param name="ids"></param>
		/// <param name="isEconomyLine"></param>
		/// <returns></returns>
		public static FrmBsdSettlementBatch Create()
		{
			var frm = new FrmBsdSettlementBatch();
			return frm;
		}

		#endregion

		#region -- 窗体事件 --

		protected override void InitComponent()
		{
			base.InitComponent();

			FrmStartTime.CustomFormat = "yyyy-MM-dd HH:mm:ss";
			FrmEndTime.CustomFormat = "yyyy-MM-dd HH:mm:ss";
			FrmStartTime.ValueObject = DateTime.Now;
			FrmEndTime.ValueObject = DateTime.Now;
		}

		//public override void FormOnShown()
		//{
		//	base.FormOnShown();
		//}

		#endregion

		private void btnSave_Click(object sender, EventArgs e)
		{
			var startTime = FrmStartTime.Value;
			var endTime = FrmEndTime.Value;
			if (startTime > endTime)
			{
				var temp = endTime;
				endTime = startTime;
				startTime = temp;
			}
			try
			{
				lock (BsdMisManagement.LockSynchronizer)
				{
					BsdMisManagement.Client.BatchUpdateSettlements(startTime, endTime);
				}
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			catch (Exception ex)
			{
				ProcessException(ex);
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
	}
}